#!/bin/sh 

################################################################################
# Copyright 1999-2010 by Nice, srl.,
# Via Marchesi di Roero, 1
# 14020 Cortanze (AT) - ITALY
# All rights reserved.
#
# This software is the confidential and proprietary information
# of Nice, srl. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with Nice.
################################################################################

. "${EF_ROOT}/plugins/irods/bin/common"
. "${EF_ROOT}/plugins/fm/fm/backends/common"

if [ -z "$1" ]; then
  fatal "The directory contents could not be displayed." "Missing input parameter: current path"
fi

_canonicalpath=`"${EF_ROOT}/plugins/fm/lib/realpath" "$1"`

if [ "${_canonicalpath}" == "/" ]; then
  _fullpath="${FM_VROOT_PATH}"
else
  _fullpath="${_canonicalpath}"
fi

path="${_fullpath}"
export path

OLD_LANG="${LANG}"
LANG=C
export LANG

OLD_LC_ALL="${LC_ALL}"
LC_ALL=C
export LC_ALL

case "$sortBy" in
size:asc)
    listing_options_suffix=" | sort -n -k 4"
    ;;
size:desc)
    listing_options_suffix=" | sort -n -r -k 4"
    ;;
modified:asc)
    listing_options_suffix=" | sort -n -k 5"
    ;;
modified:desc)
    listing_options_suffix=" | sort -n -r -k 5"
    ;;
name:desc)
    listing_options_suffix=" | sort -n -k 7"
    ;;
esac

# The 'l' option is used to list output; the 'L' option is used to dereference symbolic links
_listing_options="-l ${_fullpath} | sed 1d ${listing_options_suffix}"

_cmd="${IRODS_ICOMMANDS_DIR}/ils ${_listing_options}"


_dirs=`ils  -l | grep "C-" | awk '{print $2;}'`
echo "${_dirs}" | while read _dir ; do
  dirname=`basename "${_dir}"`
done


eval "${_cmd}" 2> /dev/null | \
  "${EF_ROOT}/plugins/irods/lib/ils2xml" \
    --vroot "${vroot}" \
    --path "${path}" \
    --target "file" \
    --sortby "${sortBy}" \
    --filter "${filter}" \
    --sorted "true" \
    --filtered "false"

_res=$?

LANG="${OLD_LANG}"
export LANG

LC_ALL="${OLD_LC_ALL}"
export LC_ALL

if [ ${_res} -ne 0 ]; then
  _canonicalpath=`"${EF_ROOT}/plugins/fm/lib/realpath" "$1"`
  error "The contents of directory \"${_canonicalpath}\" could not be displayed." "An error occurred listing the directory contents."
fi

